package com.qinghe.pro.controller.main;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qinghe.pro.entity.main.Module;
import com.qinghe.pro.entity.main.Role;
import com.qinghe.pro.service.main.ModuleService;
import com.qinghe.pro.service.main.RoleService;
import com.qinghe.utils.AjaxObject;

@Controller
@RequestMapping("/manager/role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private ModuleService moduleService;

	private static final String LIST = "manager/role/list";
	private static final String UPDATE = "manager/role/update";

	/**
	 * 得到所有角色
	 * 
	 * @author longfei
	 */
	@RequiresPermissions("Permission:show")
	@RequestMapping(value="/list", method = RequestMethod.GET)
	public String list(Map<String, Object> map) {
		List<Role> roles = roleService.findAll();

		map.put("roles", roles);
		return LIST;
	}

	/**
	 * 进入设置权限页面
	 * 
	 * @author longfei
	 */
	@RequiresPermissions("Permission:edit")
	@RequestMapping(value="/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable Integer id, Map<String, Object> map) {
		Module module = moduleService.getTree();
		Role role = roleService.get(id);

		map.put("module", module);
		map.put("role", role);
		return UPDATE;
	}

	/**
	 * 设置权限
	 * 
	 * @author longfei
	 */
	@RequiresPermissions("Permission:edit")
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public @ResponseBody AjaxObject update(Role role) {
		roleService.update(role);

		return AjaxObject.newOk("权限配置成功！");
	}

}
